package com.szhuang.mobilesafe.activity;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.szhuang.mobilesafe.R;
import com.szhuang.mobilesafe.receiver.MyAdmin;
import com.szhuang.mobilesafe.ui.SettingItemView;

/**
 * Created by szhuang on 2015/3/22.
 */
public class SetUpFourthPage extends GestureDetectorActivity {

    private DevicePolicyManager dpm;
    private ImageView last,next;
    SettingItemView startSafe;
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_fourth_page);
        init();
        setListener();

    }



    public void init(){

        dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        sp=getSharedPreferences("config",MODE_PRIVATE);
        startSafe= (SettingItemView) findViewById(R.id.start_safe);
        Boolean isOpen=sp.getBoolean("openSafe",false);
        startSafe.setTitle("点击开启防盗保护");
        if(isOpen){
            startSafe.setDesc("已经开启防盗保护");
            startSafe.setChecked(true);
        }else{
            startSafe.setDesc("没有开启防盗保护");
            startSafe.setChecked(false);
        }
        last= (ImageView) findViewById(R.id.backward);
        next=(ImageView)findViewById(R.id.forward);
    }

    public void setListener(){
        last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                previousStep();
            }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Boolean isOpen=sp.getBoolean("openSafe",false);
                if(isOpen){
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putBoolean("configed", true);
                    editor.commit();

                    ComponentName   who = new ComponentName(SetUpFourthPage.this,MyAdmin.class);
                    if(!dpm.isAdminActive(who)){
                        Toast.makeText(SetUpFourthPage.this, "还没有打开管理员权限", Toast.LENGTH_LONG).show();
                        openAdmin();
                    }else{
                        nextStep();
                    }
                }else{
                    Toast.makeText(SetUpFourthPage.this,"还没有开启防盗保护",Toast.LENGTH_SHORT).show();
                }
            }
        });

        startSafe.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(startSafe.isOpen()){
                    startSafe.setChecked(false);
                    startSafe.setDesc("没有开启防盗保护");
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putBoolean("openSafe",false);
                    editor.commit();
                }else{
                    startSafe.setChecked(true);
                    startSafe.setDesc("已经开启防盗保护");
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putBoolean("openSafe",true);
                    editor.commit();
                }
            }
        });
    }

    @Override
    public void previousStep() {

        Intent intent=new Intent(SetUpFourthPage.this,SetUpThirdPage.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.tran_pre_in, R.anim.tran_pre_out);
    }

    @Override
    public void nextStep() {
        Intent intent=new Intent(SetUpFourthPage.this,LostFindActivity.class);
        startActivity(intent);
        finish();

    }

    public void openAdmin(){
        /*//创建一个Intent
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        //我要激活谁
        ComponentName mDeviceAdminSample = new ComponentName(this,MyAdmin.class);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
        startActivity(intent);*/

        Intent intent=new Intent(this,TestLockScreenActivity.class);
        startActivity(intent);


    }

}
